//index.js
//获取应用实例
import {
  doLogin,
  register,
  orderRu,
  orderZhan,
  getUserStatus,
  admindoLogin,
  confirmZhan,
  del,
  confirmRu
} from '../../utils/login/index'
const app = getApp()

Page({
  data: {
    show: true,
    appId: "wxb2fbb734af59e1f5",
    status: "",
    btnshow: true
  },


  init() {
    // 获取当前状态
    getUserStatus({
      id: wx.getStorageSync('token')
    }).then(res => {
      console.log(res);
      if (res.data.data.status == 0) {
        this.setData({
          status: '初始状态,可以申请状态',
          btnshow: true
        })
      } else if (res.data.data.status == 1) {
        this.setData({
          status: '发送占位申请',
          btnshow: true
        })
      } else if (res.data.data.status == 2) {
        this.setData({
          status: '占位申请通过 ,可以进行入学申请; ',
          btnshow: false
        })
      } else if (res.data.data.status == 3) {
        this.setData({
          status: '发送入学申请 '
        })
      } else if (res.data.data.status == 4) {
        this.setData({
          status: '入学申请通过',
          btnshow: false
        })
      } else if (res.data.data.status == 5) {
        this.setData({
          status: '已毕业 ',
          btnshow: false
        })
      }
    })
  },


  // 用户登录
  userlogin() {

    // register({
    //   appId:'wxb2fbb734af59e1f5',
    //   appSecret:'c58259e5ebbc8c95fa9e3bad0d87bac9'
    // }).then(res=>{
    //   console.log(res)
    // })

    var _this = this
    wx.login({
      success(res) {
        console.log(res);
        var code = res.code;
        doLogin({
          code: code,
          appid: "wxb2fbb734af59e1f5"
        }).then(res => {
          console.log(res.data.data.id)
          if (res.data.data.id) {
            wx.setStorage({
              key: 'token',
              data: res.data.data.id
            })
            _this.setData({
              show: false
            })
          }
        })
      }
    })
    this.setData({
      status: "初始状态,可以申请状态",
      btnshow: true
    })
  },

  // 到占座页面
  tosit() {
    wx.navigateTo({
      url: '/pages/sit/index',
    })
  },
  // 到入学申请页面
  tostudy() {
    wx.navigateTo({
      url: '/pages/study/index',
    })
  },
  // 到信息明细页面
  touserinfo() {
    wx.navigateTo({
      url: '/pages/userinfo/index',
    })
  },
  getstate() {
    this.init()
  },
  onShow() {
    var token = wx.getStorageSync("token")
    console.log(token);
    if (token) {
      this.setData({
        show: false
      })
    } else {
      this.setData({
        show: true
      })
    };
    this.init()
  },
  // admin登录
  logins() {
    wx.removeStorage({
      key: 'bufan-token',
    })
    admindoLogin({
      username: 'admin',
      password: '123456'
    }).then(res => {
      console.log(res.data.token);
      wx.setStorageSync('bufan-token', res.data.token)
    })
  },
  //占座申请确认
  confirmZhan() {
    var _this = this
    wx.request({
      url: 'http://bufantec.com/admin/xy/student/confirmZhan',
      data: {
        id: wx.getStorageSync('token')
      },
      header: {
        // 传递的自定义请求头
        "bufan-token": wx.getStorageSync('bufan-token')
      },
      success(res) {
        console.log(res);
        if (res.data.code == 'success') {
          _this.setData({
            status: '占位申请通过 ,可以进行入学申请',
            btnshow:false
          })
            wx.showToast({
              title: "确认占座成功",
              duration: 1000
            })
        }
      }
    })
    // this.init();
  },

  //入学申请确认
  confirmRu() {
    var _this = this
    wx.request({
      url: 'http://bufantec.com/admin/xy/student/confirmRu',
      data: {
        id: wx.getStorageSync('token')
      },
      header: {
        // 传递的自定义请求头
        "bufan-token": wx.getStorageSync('bufan-token')
      },
      success(res) {
        console.log(res);
        if (res.data.code == 'success') {
          _this.setData({
            status: '入学申请通过',
            btnshow:false
          })
          wx.showToast({
            title: "确认入学成功",
            duration: 1000
          })
        }
      }
    })
    // this.init()
  },


  // 清除用户状态
  clear() {
    var _this = this
    del({
      id: wx.getStorageSync('token')
    }).then(res => {
      console.log(res);
      this.setData({
        show: true,
      })
      wx.removeStorage({
        key: 'token',
      })
    })
    // this.init()
  }
})